package com.fms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fms.entity.Discard;
import com.fms.entity.WarehouseBoard;
import com.fms.entity.WarehouseHardware;
import com.fms.mapper.DiscardMapper;
import com.fms.mapper.WarehouseBoardMapper;
import com.fms.mapper.WarehouseHardwareMapper;
import com.fms.result.ResultData;
import com.fms.service.DiscardService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class DiscardServiceImpl extends ServiceImpl<DiscardMapper, Discard> implements DiscardService {
    private final DiscardMapper discardMapper;

    private final WarehouseHardwareMapper warehouseHardwareMapper;

    private final WarehouseBoardMapper warehouseBoardMapper;
    //新增或更新报废单
    @Override
    public ResultData addDiscard(Discard discard) {
        if ("板子".equals(discard.getDiscardType())) {
            discard.setDetail(discard.getDiscardType()+": "+discard.getBoardType()+" "+discard.getBoardBase()+" "+discard.getBoardColor()+" | 数量："+discard.getBoardAmount()+"id"+discard.getDiscardId());
        }else {
            discard.setDetail(discard.getDiscardType()+": "+discard.getHardwareSpecs()+" "+discard.getHardwareBrand()+" "+discard.getHardwareSpecial()+" | 数量："+discard.getHardwareAmount()+"id"+discard.getDiscardId());
        }
        discard.setStatus(1);
        if (discard.getId() == null) {
            discardMapper.insert(discard);
            return ResultData.success("报废单添加成功");
        }else {
            discardMapper.updateById(discard);
            return ResultData.success("报废单更新成功");
        }
    }

    //获取所有报废单
    @Override
    public ResultData getDiscards() {
        LambdaQueryWrapper<Discard> discardWrapper = new LambdaQueryWrapper<>();
        List<Discard> discards = discardMapper.selectList(discardWrapper);
        return ResultData.success(discards);
    }

    //删除报废单
    @Override
    public ResultData deleteDiscard(Discard discard) {
        discardMapper.deleteById(discard);
        return ResultData.success("删除报废单成功");
    }

    @Override
    public ResultData commitDiscard(Discard discard) {
        if ("板子".equals(discard.getDiscardType())) {
            LambdaQueryWrapper<WarehouseBoard> boardWrapper = new LambdaQueryWrapper<>();
            boardWrapper.eq(WarehouseBoard::getBoardSpecs, discard.getBoardSpecs()).eq(WarehouseBoard::getBoardBase, discard.getBoardBase())
                    .eq(WarehouseBoard::getBoardColor, discard.getBoardColor()).eq(WarehouseBoard::getBoardType, discard.getBoardType());
            WarehouseBoard board = warehouseBoardMapper.selectOne(boardWrapper);
            if(board == null){
                return ResultData.error("仓库没有此板子材料");
            }
            discard.setDetail(discard.getDiscardType()+": "+discard.getBoardType()+" "+discard.getBoardBase()+" "+discard.getBoardColor()+" | 数量："+discard.getBoardAmount()+"id"+discard.getDiscardId());
        }else {
            LambdaQueryWrapper<WarehouseHardware> hardwareWrapper = new LambdaQueryWrapper<WarehouseHardware>();
            hardwareWrapper.eq(WarehouseHardware::getHardwareBrand,discard.getHardwareBrand()).eq(WarehouseHardware::getHardwareSpecial, discard.getHardwareSpecial())
                    .eq(WarehouseHardware::getHardwareSpecs,discard.getHardwareSpecs()).eq(WarehouseHardware::getHardwareType, discard.getHardwareType());
            WarehouseHardware hardware = warehouseHardwareMapper.selectOne(hardwareWrapper);
            if (hardware == null){
                return ResultData.error("仓库没有此五金材料");
            }
            discard.setDetail(discard.getDiscardType()+": "+discard.getHardwareSpecs()+" "+discard.getHardwareBrand()+" "+discard.getHardwareSpecial()+" | 数量："+discard.getHardwareAmount()+"id"+discard.getDiscardId());
        }
        discard.setStatus(discard.getStatus()+1);
        discardMapper.updateById(discard);
        if(discard.getStatus() == 2) {
            return ResultData.success("提交成功");
        }else if(discard.getStatus() == 3) {
            return ResultData.success("审核通过");
        }else {
            if ("板子".equals(discard.getDiscardType())){
                LambdaQueryWrapper<WarehouseBoard> boardBWrapper = new LambdaQueryWrapper<>();
                boardBWrapper.eq(WarehouseBoard::getBoardSpecs, discard.getBoardSpecs()).eq(WarehouseBoard::getBoardBase, discard.getBoardBase())
                        .eq(WarehouseBoard::getBoardColor, discard.getBoardColor()).eq(WarehouseBoard::getBoardType, discard.getBoardType());
                WarehouseBoard bBoard = warehouseBoardMapper.selectOne(boardBWrapper);
                warehouseBoardMapper.updateWarehouseBoardOutAmount(discard.getBoardAmount(),bBoard.getId());
            }else {
                LambdaQueryWrapper<WarehouseHardware> hardwareBWrapper = new LambdaQueryWrapper<WarehouseHardware>();
                hardwareBWrapper.eq(WarehouseHardware::getHardwareBrand,discard.getHardwareBrand()).eq(WarehouseHardware::getHardwareSpecial, discard.getHardwareSpecial())
                        .eq(WarehouseHardware::getHardwareSpecs,discard.getHardwareSpecs()).eq(WarehouseHardware::getHardwareType, discard.getHardwareType());
                WarehouseHardware bHardware = warehouseHardwareMapper.selectOne(hardwareBWrapper);
                warehouseHardwareMapper.updateWarehouseHardwareOutAmount(discard.getHardwareAmount(),bHardware.getId());
            }
            return ResultData.success("成功确认材料报废");
        }
    }

    //拒绝报废单
    @Override
    public ResultData denyDiscard(Discard discard) {
        if ("板子".equals(discard.getDiscardType())) {
            discard.setDetail(discard.getDiscardType()+": "+discard.getBoardType()+" "+discard.getBoardBase()+" "+discard.getBoardColor()+" | 数量："+discard.getBoardAmount()+"id"+discard.getDiscardId());
        }else {
            discard.setDetail(discard.getDiscardType()+": "+discard.getHardwareSpecs()+" "+discard.getHardwareBrand()+" "+discard.getHardwareSpecial()+" | 数量："+discard.getHardwareAmount()+"id"+discard.getDiscardId());
        }
        discard.setStatus(0);
        discardMapper.updateById(discard);
        return ResultData.success("已拒绝报废单");
    }
}
